package cn.gxa.j229.bug.finance.controller.borrowing;

import cn.gxa.j229.bug.finance.pojo.InvestmentRecord;
import cn.gxa.j229.bug.finance.service.borrowing.InvestmentRecordService;
import cn.gxa.j229.bug.finance.utils.JsonResult;
import cn.gxa.j229.bug.finance.utils.PageUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Result;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * 投资记录页面接口
 *
 * @Author FanJiang
 * @Date 2020/9/25
 * @Version 1.0
 */

@Controller
@RequestMapping("/investment")
public class InvestmentRecordController {

    @Resource
    InvestmentRecordService investmentRecordService;

    /**
     * 分页查询的接口，工前端页面展示使用
     *
     * @param investmentRecord 接收一个json，格式例如{"limit":"2","page":"2"}
     *                         {"entitle":"新手1号","phone":"13800009999","state":"6","limit":"1","page":"2"}
     *                         {"entitle":"新手","phone":"1","limit":"1","page":"5"}
     * @return 返回一个json给前端
     */

    @PostMapping("/findAllPage")
    @ResponseBody
    public Map findAll(@RequestBody InvestmentRecord investmentRecord) {
        List allPage = investmentRecordService.findAllPage(investmentRecord);
        Integer counts = investmentRecordService.countPage(investmentRecord);
        JsonResult jsonResult = new JsonResult();
        jsonResult.put("count", counts);
        jsonResult.setData(allPage);
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getValues();

    }

    /**
     * 导出表格的接口，目前不支持自定义列
     *
     * @param req
     * @param resp
     * @return
     */
    //导出表格
    @GetMapping("/export")
    @ResponseBody
    public Map export(HttpServletRequest req, HttpServletResponse resp) {
        try {
            //2.创建XSSFWorkbook
            XSSFWorkbook workbook = new XSSFWorkbook();
            investmentRecordService.export(workbook);

            //告诉浏览器以下载的方式代开
            resp.setHeader("Content-Disposition", "attachment;filename=" +
                    URLEncoder.encode("投资记录表.xlsx", "UTF-8"));
            ServletOutputStream out = resp.getOutputStream();
            workbook.write(out);

            JsonResult jsonResult = new JsonResult();
            jsonResult.setCode("200");
            jsonResult.setMsg("导出表格成功");

            return jsonResult.getValues();
        } catch (Exception e) {
            e.printStackTrace();
            JsonResult jsonResult = new JsonResult();
            jsonResult.setCode("50050");
            jsonResult.setMsg("导出表格失败");

            return jsonResult.getValues();
        }

    }

}
